package userService

import (
	"fmt"
	"news_for_deepseek_interface/model"
	adminModel "news_for_deepseek_interface/model/admin"
)

func IsSuperAdmin(adminId int) (isSuperAdmin bool, err error) {

	db := model.GetInstance().GetDB()

	// 查询管理员
	var adminWithGroup []int
	if err = db.Model(&adminModel.AdminWithGroupStruct{}).
		Where("admin_id = ?", adminId).
		Select("admin_group_id").
		Scan(&adminWithGroup).Error; err != nil {
		err = fmt.Errorf("获取管理员权限组失败%s", err.Error())
		return
	}

	// 管理员没有分组
	if len(adminWithGroup) == 0 {
		isSuperAdmin = false
		return
	}

	// 获取管理员权限组
	var adminGroup []adminModel.AdminGroupStruct
	if err = db.Model(&adminModel.AdminGroupStruct{}).
		Where("id in ?", adminWithGroup).
		Find(&adminGroup).Error; err != nil {
		err = fmt.Errorf("获取管理员权限组失败%s", err.Error())
		return
	}

	// 管理员组菜单为*
	for _, v := range adminGroup {
		if v.Rules == "*" {
			isSuperAdmin = true
			return
		}
	}

	isSuperAdmin = false
	return
}
